﻿/// <reference path="../../../../Scripts/jquery-1.4.1.min-vsdoc.js" />
function Validator(){}
Validator.prototype.Success = true;
Validator.prototype.Execute = null;
Validator.prototype.Message = null;
//值不为空
Validator.prototype.NotNull=function(value) {
    this.Success= value != null && value != '' && value != undefined;
}
//正则匹配
Validator.prototype.MatchRegex= function(value, parent) {
    var reg =  new RegExp(parent);
    reg.ignoreCase = true;
    if (value.match(reg) == null) {
        this.Success= false;
    }
    else{
        this.Success= true;
    }
}
//执行验证
Validator.prototype.Do=function(call)
{
    this.Success = true;
    if(this.Execute !=null)
      this.Execute();
}
//字符长度区间
Validator.prototype.LengthRegion= function(value,min,max)
{
    if(!value)
    {
         this.Success= true;
    }
    else
    {
         var lvalue = value.toString().length;
         var min = __ValidatorConvert(min,"number");
         var max = __ValidatorConvert(max,"number");
         if(min !=null && max !=null)
             this.Success= __CompareValue(lvalue,min,"req")&& __CompareValue(lvalue,max,"leq");
          else
             this.Success= __CompareValue(lvalue,min,"req")|| __CompareValue(lvalue,max,"leq");
    }
}
//数字大小区间
Validator.prototype.NumberRegion =function(value,min,max) {
    if(!value)
    {
        this.Success= true;
    }
    else
    {
        var lvalue = __ValidatorConvert(value,"number");
        if(!lvalue)
        {
           this.Success= false;
        }
        else
        {        
            var min = __ValidatorConvert(min,"number");
            var max = __ValidatorConvert(max,"number");           
            if(min !=null && max !=null)
                this.Success= __CompareValue(lvalue,min,"req")&& __CompareValue(lvalue,max,"leq");
            else
                this.Success= __CompareValue(lvalue,min,"req")|| __CompareValue(lvalue,max,"leq");
        }
    }
}
//比较数字
Validator.prototype.NumberCompare = function(leftvalue,rightvalue,type){
    var lvalue = __ValidatorConvert(leftvalue,"number");
    var rvalue = __ValidatorConvert(rightvalue,"number");
    this.Success= __CompareValue(lvalue,rvalue,type);
}
//日期大小区间
Validator.prototype.DataRegion = function(value,min,max)
{
    if(!value)
    {
        this.Success= true;
    }
    else
    {
        var lvalue = __ValidatorConvert(value,"date");
        if(!lvalue)
        {
            this.Success = false;
        }
        else
        {
            var min = __ValidatorConvert(min,"date");
            var max = __ValidatorConvert(max,"date");
            if(min !=null && max !=null)
                this.Success= __CompareValue(lvalue,min,"req")&& __CompareValue(lvalue,max,"leq");
            else
                this.Success= __CompareValue(lvalue,min,"req")|| __CompareValue(lvalue,max,"leq");
        }
    }
}
//比较日期
Validator.prototype.DateCompare = function(leftvalue,rightvalue,type){
        var lvalue = __ValidatorConvert(leftvalue,"Date");
        var rvalue = __ValidatorConvert(rightvalue,"Date");
        this.Success= __CompareValue(lvalue,rvalue,type);
}
//匹配邮件
Validator.prototype.EMail = function(value){
    this.MatchRegex(value,'^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$');
}
//字符比较
Validator.prototype.StringCompare = function(leftvalue,rightvalue,type){
    this.Success= __CompareValue(leftvalue,rightvalue,type);
}
//选择项
Validator.prototype.SelectCheckbox= function(controls,selectcount)
{
    var selects =0;
    for(var i =0;i<controls.length;i++)
    {
       if($('#'+controls[i]).attr('checked'))
       {
            selects++;
       }
    }
    this.Success= selects>= selectcount;
}
//选择项
Validator.prototype.SelectCheckboxFromTo=function(control,from,to,selectcount)
{
    var selects =0;
    for(var i =from;i<to;i++)
    {
       if($('#'+controls[i]).attr('checked'))
       {
            selects++;
       }
    }
    this.Success= selects>= selectcount;
}
//值是否为null
function __IsNull(value)
{
     if(!value)
        return true;
    return false;
}
//比较值函数
function __CompareValue(leftvalue,rightvalue,compareType)
{
    if(__IsNull(leftvalue)  || __IsNull(rightvalue))
        return false;
    if(compareType=="eq")
    {
        return leftvalue == rightvalue;
    }
    else if(compareType =="neq")
    {
        return leftvalue != rightvalue;
    }
    else if(compareType =="le")
    {
        return leftvalue < rightvalue;
    }
    else if(compareType =="leq")
    {
        return leftvalue <= rightvalue;
    }
    else if(compareType =="ri")
    {
        return leftvalue > rightvalue;
    }
    else if(compareType =="req")
    {
        return leftvalue >= rightvalue;
    }
    else
    {
        return false;
    }    
}
//转换值函数
function __ValidatorConvert(value, dataType) {
    var num,exp,m;
    var year,month,day
    if(value == null || value =="")
        return null;
    if(dataType=="int")
    {
        exp=/^[-\+]?\d+$/;
        if (value.match(exp) == null)
            return null;
        num = parseInt(value, 10);
        return (isNaN(num) ? null : num);
    }
    else if(dataType =="number")
    {
        exp=/^[-\+]?((\d+)|(\d+\.\d+))$/;
        if (value.match(exp) == null)
            return null;
        num = parseFloat(value);
        return (isNaN(num) ? null : num);
    }
    else if(dataType =="date")
    {
        exp=/^(\d{4})([-/]?)(\d{1,2})([-/]?)(\d{1,2})$/
        m = value.match(exp);
        if (m == null)
        {
            exp=/^(\d{1,2})([-/]?)(\d{1,2})([-/]?)(\d{4})$/
            m = value.match(exp);
            if(m== null)
                return null;
            year = m[5];
            month = m[1];
            day =m[3];            
        }
        else
        {
            year = m[1];
            month =m[3];
            day = m[5];
        }
        try
        {
            num = new Date(year,month,day);
        }
        catch(e)
        {
            return null;
        }
        return num;
    }
    else
    {
        return value.toString();
    }
}
